package cn.doctorpda.conference.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import cn.doctorpda.conference.R;
import cn.doctorpda.conference.event.GetMessage;
import cn.doctorpda.conference.ui.checked.CheckFragment;
import cn.doctorpda.conference.ui.info.InfoFragment;
import cn.doctorpda.conference.ui.personal.PersonalFragment;
import cn.doctorpda.conference.ui.recommend.RecommendFragment;
import cn.doctorpda.conference.ui.search.SearchActivity;
import cn.doctorpda.conference.update.BaiyyyUpdateAgent;

/**
 * Created by huangjinlong on 2016/5/13.
 */
public class MainFragment extends BaseFragment implements View.OnClickListener {

    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> fragments = new ArrayList<>();
    private TabLayout mTabLayout;
    private String[] titles = new String[]{"推荐", "查会", "资料", "我的"};

    public static MainFragment newInstance() {
        MainFragment fragment = new MainFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        initView(view);
        initData();
        EventBus.getDefault().register(this);

        BaiyyyUpdateAgent.checkUpdate(getActivity());

        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //首页联动
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSetTabEvent(GetMessage tabEvent) {
        if (tabEvent.getMessage().equals("checkMeeting")) {
            mViewPager.setCurrentItem(1);
        } else if (tabEvent.getMessage().equals("toDoc")) {
            mViewPager.setCurrentItem(2);
        }
    }


    private void initData() {
        fragments.add(RecommendFragment.newInstance());
        fragments.add(CheckFragment.newInstance());
        fragments.add(InfoFragment.newInstance());
        fragments.add(PersonalFragment.newInstance());
        mAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }

        };
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(0);
        mViewPager.setOffscreenPageLimit(4);
        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);

        setupTabIcons();
        mTabLayout.getTabAt(0).getCustomView().setSelected(true);
    }

    private void setupTabIcons() {
        View tabOne = LayoutInflater.from(getActivity()).inflate(R.layout.item_home_title, null);
        ImageView oneIv = (ImageView) tabOne.findViewById(R.id.iv_item);
        TextView oneTv = (TextView) tabOne.findViewById(R.id.tv_item);
        oneIv.setImageResource(R.drawable.item_home_title_recommend);
        oneTv.setText(titles[0]);
        mTabLayout.getTabAt(0).setCustomView(tabOne);
        View tabTwo = LayoutInflater.from(getActivity()).inflate(R.layout.item_home_title, null);
        ImageView twoIv = (ImageView) tabTwo.findViewById(R.id.iv_item);
        TextView twoTv = (TextView) tabTwo.findViewById(R.id.tv_item);
        twoIv.setImageResource(R.drawable.item_home_title_check);
        twoTv.setText(titles[1]);
        mTabLayout.getTabAt(1).setCustomView(tabTwo);
        View tabThree = LayoutInflater.from(getActivity()).inflate(R.layout.item_home_title, null);
        ImageView threeIv = (ImageView) tabThree.findViewById(R.id.iv_item);
        TextView threeTv = (TextView) tabThree.findViewById(R.id.tv_item);
        threeIv.setImageResource(R.drawable.item_home_title_info);
        threeTv.setText(titles[2]);
        mTabLayout.getTabAt(2).setCustomView(tabThree);
        View tabFour = LayoutInflater.from(getActivity()).inflate(R.layout.item_home_title, null);
        ImageView fourIv = (ImageView) tabFour.findViewById(R.id.iv_item);
        TextView fourTv = (TextView) tabFour.findViewById(R.id.tv_item);
        fourIv.setImageResource(R.drawable.item_home_title_personal);
        fourTv.setText(titles[3]);
        mTabLayout.getTabAt(3).setCustomView(tabFour);
    }

    private void initView(View view) {
        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        view.findViewById(R.id.tv_to_search).setOnClickListener(this);//搜索

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.tv_to_search:
                Intent toSearch = new Intent();
                toSearch.setClass(getActivity(), SearchActivity.class);
                startActivity(toSearch);
                break;

        }

    }

    @Override
    public String getPageName() {
        return "MainFragment";
    }
}
